package com.zkingsoft.web.action;

import com.matrix.core.anotations.RemoveRequestToken;
import com.matrix.core.anotations.SaveRequestToken;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.common.constance.AppConstance;
import com.zkingsoft.web.bean.WebArticle;
import com.zkingsoft.web.service.WebArticleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @date 2016-12-01 10:36
 */
@Controller
@RequestMapping(value = "admin/webArticle")
public class WebArticleController {

	@Resource
	private WebArticleService webArticleService;

	// 记录编辑前的值Before_Edit_Value
	public static final String BEV = "WebArticle_BEV";

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(WebArticle webArticle, PaginationVO pageVo) {
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		webArticle.setCompanyId(user.getCompanyId());
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS,  
				webArticleService.findInPage(webArticle, pageVo), webArticleService.findTotal(webArticle));
		return result;

	}

	/**
	 * 新增或者修改页面
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(WebArticle webArticle) {
		if (webArticle.getArtId() != null) {

			int i = webArticleService.modifyByMap(WebUtil.getSessionAttribute(BEV), webArticle);
			WebUtil.removeSessionAttribute(BEV);
			if (i > 0) {
				return new AjaxResult(AjaxResult.STATUS_SUCCESS,  "文章修改成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_FAIL,  "文章修改失败");
			}
		} else {
			webArticle.setArtCreatetiem(new Date());
			SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
			webArticle.setCompanyId(user.getCompanyId());
			int i = webArticleService.add(webArticle);
			if (i > 0) {
				return new AjaxResult(AjaxResult.STATUS_SUCCESS,  "文章新增成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_FAIL,  "文章新增失败");
			}
		}
	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		WebArticle webArticle;
		if (id != null) {
			webArticle = webArticleService.findById(id);
			WebUtil.getRequest().setAttribute("obj", webArticle);
			WebUtil.setSessionAttribute(BEV, webArticle);
		}
		return "admin/web/article-form";
	}

	/**
	 * 删除
*/
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		List<Long> ids = StringUtils.strToCollToLong(keys, ",");
		int i = webArticleService.remove(ids);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS,  "成功删除" + i + "条数据");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL,  "删除失败");
		}
	}

	/**
	 * 发布文章或者文章下架
	 */
	@RequestMapping(value = "/publish")
	public @ResponseBody AjaxResult publish(Long id, Integer status) {
		String msg;
		WebArticle webArticle = webArticleService.findById(id);
		if (webArticle == null || status == null) {
			throw new GlobleException("无效数据");
		}
		if (status == 1) {
			msg = "发布成功";
			webArticle.setArtIsPublish(AppConstance.IS_Y);
		} else if (status == 2) {
			msg = "撤回成功";
			webArticle.setArtIsPublish(AppConstance.IS_N);
		} else {
			throw new GlobleException("无效数据");
		}
		int i = webArticleService.modifyByModel(webArticle);
		return i > 0 ? new AjaxResult(AjaxResult.STATUS_SUCCESS, msg) : new AjaxResult(AjaxResult.STATUS_FAIL, msg);
	}

}